<template>
  <div>
    <textarea
      id="copy"
      style="position: absolute; left: -999999px; top: -999999px"
    ></textarea>

    <el-row>
      <el-col :span="6"
        ><el-input
          v-model="fileName"
          @input="changeInput"
          placeholder="请输入原文件名"
      /></el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" row-key="id" border>
          <el-table-column type="expand">
            <template v-slot="props">
              <el-table :data="props.row.child" row-key="id" border>
                <el-table-column prop="createTime" label="创建时间" />
                <el-table-column label="下载名称">
                  <template v-slot="scope">
                    {{
                      scope.row.downloadFileName ||
                      props.row.fileName.split("/")[
                        props.row.fileName.split("/").length - 1
                      ]
                    }}
                  </template>
                </el-table-column>
                <el-table-column label="下载速度">
                  <template v-slot="scope">
                    {{ scope.row.downloadSpeed || "无限制" }}
                  </template>
                </el-table-column>
                <el-table-column label="过期时间">
                  <template v-slot="scope">
                    {{ scope.row.expirationTime || "无限制" }}
                  </template>
                </el-table-column>
                <el-table-column prop="createTime" label="操作">
                  <template v-slot="scope">
                    <el-button size="mini" @click="copyItme(scope.row.id)"
                      >地址复制</el-button
                    >
                    <el-button
                      size="mini"
                      type="success"
                      v-show="
                        scope.row.downloadSpeed != 0 && scope.row.expirationTime
                      "
                      @click="editItem(scope.row)"
                      >修改</el-button
                    >
                    <el-button
                      size="mini"
                      type="danger"
                      @click="deleteItem(scope.row)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
          <el-table-column prop="oldFileName" label="源文件名" />
          <el-table-column prop="createTime" label="创建时间" />
          <el-table-column prop="createTime" label="操作">
            <template v-slot="scope">
              <el-button
                size="mini"
                @click="
                  fileListId = scope.row.id;
                  isShow = true;
                "
                >下载添加</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
            :total="page.total"
          ></el-pagination>
        </div>
      </el-col>
    </el-row>
    <DownloadDialog
      title="添加"
      v-model="isShow"
      @submit="submitAdd"
    ></DownloadDialog>
    <DownloadDialog
      v-if="isShowEdit"
      title="修改"
      :defaultValue="editFrom"
      v-model="isShowEdit"
      @submit="submitEdit"
    ></DownloadDialog>
  </div>
</template>

<script>
import { onMounted, reactive, toRefs } from "vue";
import DownloadDialog from "./downloadDialog.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { debounce } from "@/utils/utils";
import {
  fileList,
  downLoadPathStr,
  downloadFileAdd,
  deleteDownloadFile,
  updateDonwloadFile,
} from "@/api/article.js";
export default {
  components: { DownloadDialog },
  setup() {
    const data = reactive({
      isShow: false,
      isShowEdit: false,
      tableData: [],
      fileListId: "",
      fileName: "",
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      editFrom: {},
    });
    const debounceGetData = debounce(getData, 300);
    onMounted(() => {
      getData();
    });
    function handleCurrentChange(e) {
      data.page.pageNum = e;
      getData();
    }
    function getData() {
      fileList({
        pageNum: data.page.pageNum,
        pageSize: data.page.pageSize,
        oldFileName: data.fileName,
      }).then((res) => {
        if (res.code == 200) {
          data.tableData = res.data.data;
          data.page.total = res.data.total;
        }
      });
    }
    function changeInput() {
      debounceGetData();
    }
    function copyItme(item) {
      var text = "";
      text = downLoadPathStr(item);
      var input = document.getElementById("copy");
      input.value = text;
      input.select();
      document.execCommand("copy"); // 执行浏览器复制命令
      ElMessage.success("复制成功");
    }
    function submitAdd({ item, clear }) {
      downloadFileAdd({ fileListId: data.fileListId, ...item }).then((res) => {
        if (res.code == 200) {
          ElMessage({
            message: "添加成功！",
            type: "success",
          });
          getData();
          clear();
        }
        data.isShow = false;
      });
    }
    function deleteItem(item) {
      ElMessageBox.confirm("此操作将永久删除，是否继续?", "Warning", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteDownloadFile({ downloadFileId: item.id })
            .then((res) => {
              if (res.code == 200) {
                ElMessage({
                  type: "success",
                  message: "删除成功！",
                });
                getData();
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    }
    function editItem(item) {
      data.editFrom = item;
      data.isShowEdit = true;
    }
    function submitEdit({ item }) {
      updateDonwloadFile({
        downloadFileId: data.editFrom.id,
        downloadFileName: item.downloadFileName,
        downloadSpeed: item.downloadSpeed,
        expirationTime: item.expirationTime,
      })
        .then((res) => {
          if (res.code == 200) {
            ElMessage({
              type: "success",
              message: "修改成功！",
            });
            getData();
          }
          data.isShowEdit = false;
        })
        .catch(() => {
          data.isShowEdit = false;
        });
    }
    return {
      ...toRefs(data),
      submitAdd,
      deleteItem,
      editItem,
      submitEdit,
      handleCurrentChange,
      changeInput,
      copyItme,
    };
  },
};
</script>

<style lang="scss" scoped>
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.block {
  text-align: center;
}
</style>
